﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerPhanLopDao : IPhanLopDao
    {
        public SqlServerPhanLopDao()
        {
            SQLUtility.TableName = "PhanLop";
        }

        //Trả về đối tượng báo ăn
        private PhanLop MakePhanLop(DataRow row)
        {
            Guid _id = Guid.Parse(row["PhanLopID"].ToString());
            Guid _classID = Guid.Parse(row["ClassID"].ToString());
            Guid _studentID = Guid.Parse(row["StudentID"].ToString());
            Guid _schoolYearID = Guid.Parse(row["SchoolYearID"].ToString());
            DateTime _startDate = DateTime.Parse(row["StartDate"].ToString());
            DateTime _endate = DateTime.Parse(row["Endate"].ToString());

            return new PhanLop(_id, _classID, _studentID, _schoolYearID, _startDate, _endate);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<PhanLop> MakePhanLops(DataTable dt)  
        {
            IList<PhanLop> list = new List<PhanLop>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakePhanLop(row));
            }
            return list;
        }
        private DbParameter[] Take(PhanLop PhanLop)
        {
            DbParameter[] parameters = new DbParameter[6];
            parameters[0] = new SqlParameter("@PhanLopID", PhanLop.Id);
            parameters[1] = new SqlParameter("@ClassID", PhanLop.ClassID);
            parameters[2] = new SqlParameter("@StudentID", PhanLop.StudentID);

            parameters[3] = new SqlParameter("@SchoolYearID", PhanLop.SchoolYearID);
            parameters[4] = new SqlParameter("@StartDate", PhanLop.StartDate);
            parameters[5] = new SqlParameter("@Endate", PhanLop.Endate);
            return parameters;
        }
        public IList<Business_Objects.PhanLop> GetPhanLop()
        {
            SQLUtility.TableName = "PhanLop";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakePhanLops(dt);
        }

        public Business_Objects.PhanLop GetPhanLopByID(string PhanLopID)
        {
            SQLUtility.TableName = "PhanLop";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@PhanLopID", PhanLopID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakePhanLop(row);
            }
        }

        public void InsertPhanLop(Business_Objects.PhanLop PhanLop)
        {
            SQLUtility.TableName = "PhanLop";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(PhanLop), true);
        }

        public int UpdatePhanLop(Business_Objects.PhanLop PhanLop)
        {
            SQLUtility.TableName = "PhanLop";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(PhanLop)); }
            catch { return -1; }
        }

        public int DeletePhanLop(Business_Objects.PhanLop PhanLop)
        {
            SQLUtility.TableName = "PhanLop";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(PhanLop)); }
            catch { return -1; }
        }





        public List<PhanLop> GetPhanLopBySchoolIdAndSchoolYearID(string SchoolYearID, int schoolId)
        {
            DbParameter[] parameters = new DbParameter[2];
            parameters[0] = new SqlParameter("@SchoolYearID", SchoolYearID);
            parameters[1] = new SqlParameter("@SchoolID", schoolId);
            DataTable dt = CommandProvider.GetDatatable("usp_SelectPhanLopsByAndSchoolYearIDAndSchoolID", parameters);
            return MakePhanLops(dt).ToList();  
        }


        public List<PhanLop> GetPhanLopBySchoolIdAndSchoolYearIDAndClassID(string SchoolYearID, int schoolId, string ClassID)
        {
            DbParameter[] parameters = new DbParameter[2];
            parameters[0] = new SqlParameter("@SchoolYearID",Guid .Parse( SchoolYearID));
            parameters[1] = new SqlParameter("@SchoolID", schoolId);
            //parameters[2] = new SqlParameter("@ClassID", ClassID);
            DataTable dt = CommandProvider.GetDatatable("usp_SelectPhanLopsByAndSchoolYearIDAndSchoolIDAndClassID", parameters);
            return MakePhanLops(dt).ToList();  
        }


        public void  DeleteStudentFromPhanLop(string studentID)
        {
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@StudentID", Guid.Parse(studentID));
            try
            {
                CommandProvider.Delete("usp_DeletePhanLopsByAndStudentID", parameters);
            }
            catch (Exception)
            {
                
            }
        }
    }
}
